<?php
/**
 * PhpStorm
 * User: zucheng
 * Date: 2019/3/25
 */

namespace App\Traits;

use Pay;
use QrCode;
use Yansongda\Pay\Log;

trait PayHelper
{
    /**
     * 微信app支付
     * @param $tradeNo 商户订单号
     * @param $amount 金额，单位：分
     * @param $subject 支付备注
     * @return json
     */
    public function wechatPayByApp($tradeNo, int $amount, $subject)
    {
        $wechat = Pay::wechat();
        $order  = [
            'out_trade_no' => $tradeNo,
            'total_fee'    => $amount,
            'body'         => $subject,
        ];

        // 返回json格式，供后续APP调用
        return $wechat->app($order);
    }

    /**
     * 微信扫码支付
     * @param $tradeNo 商户订单号
     * @param $amount 金额，单位：分
     * @param $subject 支付备注
     * @return mixed 二维码内容
     */
    public function wechatPayByScan($tradeNo, int $amount, $subject)
    {
        $wechat = Pay::wechat();
        $order  = [
            'out_trade_no' => $tradeNo,
            'total_fee'    => $amount,
            'body'         => $subject,
        ];
        $result = $wechat->scan($order);

        // 返回二维码内容
        return $result->code_url;
    }

    /**
     * 微信小程序支付
     * @param $tradeNo 商户订单号
     * @param $amount 金额，单位：分
     * @param $subject 支付备注
     * @param $openid 用户标识
     * @return mixed 二维码内容
     */
    public function wechatPayByMiniapp($tradeNo, int $amount, $subject, $openid)
    {
        $wechat = Pay::wechat();
        $order  = [
            'out_trade_no' => $tradeNo,
            'total_fee'    => $amount,
            'body'         => $subject,
            'openid'       => $openid,
        ];
        return $wechat->miniapp($order);
    }

    /**
     * 微信全额退款
     * @param $tradeNo 商户订单号
     * @param $refundNo 商户退款单号
     * @param $refundAmount 退款金额，单位：分
     * @param $desc 退款说明
     * @return mixed
     */
    public function wechatRefund($tradeNo, $refundNo, int $amount, $desc)
    {
        $wechat = Pay::wechat();
        $order  = [
            'out_trade_no'  => $tradeNo,
            'out_refund_no' => $refundNo,
            'total_fee'     => $amount,
            'refund_fee'    => $amount,
            'refund_desc'   => $desc
        ];

        return $wechat->refund($order);
    }

    /**
     * 微信订单查询
     * @param $tradeNo 商户订单号
     * @return mixed
     */
    public function wechatOrderFind($tradeNo)
    {
        $wechat = Pay::wechat();
        $order  = [
            'out_trade_no' => $tradeNo,
        ];

        return $wechat->find($order);
    }

    /**
     * 微信退款订单查询
     * @param $tradeNo 商户订单号
     * @return mixed
     */
    public function wechatRefundOrderFind($tradeNo)
    {
        $wechat = Pay::wechat();
        $order  = [
            'out_trade_no' => $tradeNo,
        ];

        return $wechat->find($order, true);
    }

    /**
     * 支付宝app支付
     * @param $tradeNo 商户订单号
     * @param $amount 金额，单位：分
     * @param $subject 支付备注
     * @return string
     */
    public function alipayByApp($tradeNo, int $amount, $subject)
    {
        $alipay = Pay::alipay();
        $amount = $amount / 100; // 转换为元
        $order  = [
            'out_trade_no' => $tradeNo,
            'total_amount' => $amount,
            'subject'      => $subject,
        ];

        // 返回字符串，供后续APP调用
        return $alipay->app($order);
    }

    /**
     * 支付宝扫码支付
     * @param $tradeNo 商户订单号
     * @param $amount 金额，单位：分
     * @param $subject 支付备注
     * @return mixed 二维码内容
     */
    public function alipayByScan($tradeNo, int $amount, $subject)
    {
        $alipay = Pay::alipay();
        $amount = $amount / 100; // 转换为元
        $order  = [
            'out_trade_no' => $tradeNo,
            'total_amount' => $amount,
            'subject'      => $subject,
        ];
        $result = $alipay->scan($order);

        // 返回二维码内容
        return $result->qr_code;
    }

    /**
     * 支付宝退款
     * @param $tradeNo 商户订单号
     * @param $refundAmount 退款金额，单位：分
     * @return mixed
     */
    public function alipayRefund($tradeNo, int $amount)
    {
        $alipay = Pay::alipay();
        $amount = $amount / 100; // 转换为元
        $order  = [
            'out_trade_no'  => $tradeNo,
            'refund_amount' => $amount,
        ];

        return $alipay->refund($order);
    }

    /**
     * 支付宝转账
     * @param $tradeNo 商户订单号
     * @param $payeeAccount 收款方支付宝登录号：邮箱或者手机号
     * @param $amount  转账金额，单位：分，必须大于等于10分
     * @param $payeeRealName 收款方真实姓名，不为空时会验证是否和支付宝实名认证一致
     * @param $remark 转账备注 200个英文/100个汉字，当付款方为企业账户，且转账金额大于等于50000元，remark不能为空
     * @return mixed
     */
    public function alipayTransfer($tradeNo, $payeeAccount, int $amount, $payeeRealName = '', $remark = '')
    {
        $alipay = Pay::alipay();
        $amount = $amount / 100; // 转换为元
        $order  = [
            'out_biz_no'      => $tradeNo,
            'payee_type'      => 'ALIPAY_LOGONID',
            'payee_account'   => $payeeAccount,
            'amount'          => $amount,
            'payer_show_name' => config('pay.payer_show_name'),
            'payee_real_name' => $payeeRealName,
            'remark'          => $remark,
        ];

        return $alipay->transfer($order);
    }

    /**
     * 支付宝（普通订单）订单查询
     * @param $tradeNo 商户订单号
     * @return mixed
     */
    public function alipayOrderFind($tradeNo)
    {
        $alipay = Pay::alipay();
        $order  = [
            'out_trade_no' => $tradeNo,
        ];

        return $alipay->find($order);
    }

    /**
     * 支付宝退款订单查询
     * @param $tradeNo 商户订单号
     * @return mixed
     */
    public function alipayRefundOrderFind($tradeNo)
    {
        $alipay = Pay::alipay();
        $order  = [
            'out_trade_no' => $tradeNo,
        ];

        return $alipay->find($order, 'refund');
    }

    /**
     * 支付宝转账订单查询
     * @param $tradeNo 商户订单号
     * @return mixed
     */
    public function alipayTransferOrderFind($tradeNo)
    {
        $alipay = Pay::alipay();
        $order  = [
            'out_trade_no' => $tradeNo,
        ];

        return $alipay->find($order, 'transfer');
    }

    /**
     * 生成二维码
     * @param $content 二维码内容
     * @param int $size 二维码大小
     * @return string|void
     */
    public function createQr($content, $size = 200)
    {
        return QrCode::size($size)->generate($content);
    }

}